﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interactivity;
using System.Windows;

namespace Hotel.UI.Behaviors
{
//    <Button Content = "Reset" >
//    < i:Interaction.Triggers>
//        <i:EventTrigger EventName = "Click" >
//            < local:SetProperty Source = "{Binding}" PropertyName="PropertyToSet" Value="{x:Null}" />
//        </i:EventTrigger>
//    </i:Interaction.Triggers>
//</Button>

    public class SetProperty : TriggerAction<FrameworkElement>
    {
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(object), typeof(SetProperty), new PropertyMetadata(default(object)));

        /// <summary>
        /// Source is DataContext
        /// </summary>
        public object Source
        {
            get { return (object)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty PropertyNameProperty =
            DependencyProperty.Register("PropertyName", typeof(string), typeof(SetProperty), new PropertyMetadata(default(string)));

        /// <summary>
        /// Name of the Property
        /// </summary>
        public string PropertyName
        {
            get { return (string)GetValue(PropertyNameProperty); }
            set { SetValue(PropertyNameProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(SetProperty), new PropertyMetadata(default(object)));

        /// <summary>
        /// Value to Set
        /// </summary>
        public object Value
        {
            get { return (object)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        protected override void Invoke(object parameter)
        {
            if (Source == null) return;
            if (string.IsNullOrEmpty(PropertyName)) return;

            Source.GetType().GetProperty(PropertyName).SetValue(Source, Value, new object[] { });
        }
    }
}
